package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.GiveUpPresenter;
import com.example.peibei.service.presenter.MyZanDynamicPresenter;
import com.example.peibei.ui.activity.DynamicDetailActivityTwo;
import com.example.peibei.ui.adapter.DynamicZanListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyZanBillFragment extends WDFragment {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    List<Dynamic.RecordsDTO> orders;
    private DynamicZanListAdapter homeOrderListAdapter;
    private SPUtils spUtils;
    private String token;
    private MyZanDynamicPresenter myCollectOrderPresenter;
    private GiveUpPresenter giveUpPresenter;

    @Override
    public String getPageName() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_send_bill_normal;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeOrderListAdapter = new DynamicZanListAdapter(orders);
        order_list.setAdapter(homeOrderListAdapter);
        order_list.setEmptyView(empty_view);
        myCollectOrderPresenter = new MyZanDynamicPresenter(new OrderCall());
        giveUpPresenter = new GiveUpPresenter(new FollowCall());
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Dynamic.RecordsDTO data = (Dynamic.RecordsDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putSerializable("subjectId", data.getId()+"");
            bundle.putString("status", "0");
            intent(DynamicDetailActivityTwo.class, bundle);
        });
        homeOrderListAdapter.addChildClickViewIds(R.id.zan_ly);
        homeOrderListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.zan_ly) {
                    Dynamic.RecordsDTO data = (Dynamic.RecordsDTO) adapter.getData().get(position);
                    giveUpPresenter.reqeust(false, "Bearer " + token, data.getId() + "");
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        myCollectOrderPresenter.reqeust(true, "Bearer " + token);
    }

    class FollowCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            myCollectOrderPresenter.reqeust(true, "Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class OrderCall implements DataCall<Dynamic> {
        @Override
        public void success(Dynamic data, Object... args) {
            //添加列表并刷新
            if (myCollectOrderPresenter.getPage() == 1) {
                orders.clear();
            }
            homeOrderListAdapter.setNewInstance(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
